package page.pc.newListPage;

import basicTool.WaitTool;
import com.customize.reporter.WebReporter;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import page.pc.AbstractPCPage;

import java.util.List;
import java.util.Random;

public class NewListPage extends AbstractPCPage {

    @FindBy(css = ".common-wrap.cat-banner.n-banner")
    private WebElement newListPageIMG;

//    @FindBy(css = "a.cat-line-v")
//    private List<WebElement> newList;

    @FindBy(css = ".new-pcNews>li")
    private List<WebElement> companyNewsList;

    @FindBy(css = ".page .a1.a1-right")
    private WebElement turnToTheNextPageLink;

    @FindBy(css = ".page .a1.a1-left")
    private WebElement turnToTheForwardPageLink;

    @FindBy(css = ".jump-page>input[type='text']")
    private WebElement turnToThePageUserDefineText;


    public NewListPage(WebDriver driver) {
        super(driver);
        WaitTool.waitFor(driver, WaitTool.DEFAULT_WAIT_4_ELEMENT, newListPageIMG);
    }

    public NewListPage clickTurnToNextNewsPageLink() throws Exception {
        return click(turnToTheNextPageLink, NewListPage.class);
    }

    public NewListPage clickForwardToNewsPageLink() throws Exception {
        clickTurnToNextNewsPageLink();
        return click(turnToTheForwardPageLink, NewListPage.class);
    }

    public LatestCompanyNewsPage clickOneNewsRandomLink() throws Exception {
        WebReporter.log(driver, driver.getTitle(), true, true);
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        int number = new Random().nextInt(companyNewsList.size());
        System.out.println("点击公司新闻NewsList中的第：" + number + " 条新闻Link");
        jse.executeScript("document.getElementsByClassName('news-title')[" + number + "].click()");
        return new LatestCompanyNewsPage(driver);
    }

    public void setInputAndToGoDefinePage() throws Exception {
        setInputTextAndLeave(turnToThePageUserDefineText, "3");
    }

}
